package com.zhihuiyanche.service.checkcar.impl;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhihuiyanche.common.constant.Constant;
import com.zhihuiyanche.common.util.PriveKeyUtil;
import com.zhihuiyanche.dao.CustomersMapper;
import com.zhihuiyanche.dao.OrdersMapper;
import com.zhihuiyanche.dao.ShopsMapper;
import com.zhihuiyanche.dao.VehiclesMapper;
import com.zhihuiyanche.domain.CheckCar;
import com.zhihuiyanche.domain.Customers;
import com.zhihuiyanche.domain.Orders;
import com.zhihuiyanche.domain.Shops;
import com.zhihuiyanche.domain.Vehicles;
import com.zhihuiyanche.service.checkcar.CheckCarService;

@Component("checkCarService")
public class CheckCarServiceImpl implements CheckCarService {

	@Autowired
	private CustomersMapper customersMapper;

    @Autowired
    private VehiclesMapper vehiclesMapper;

    @Autowired
    private OrdersMapper ordersMapper;
    
    @Autowired
    private ShopsMapper shopsMapper;
	
	@Override
	public String insertCustomer(CheckCar checkCar) {
		String priveKey = "";
		//数据进行入库
        Customers customers = new Customers();
        customers.setName(checkCar.getName());
        customers.setMobile(checkCar.getMobile());
        
        //根据客户姓名和手机号查询客户是否存在
        List<Customers> list = queryCustomersByCondition(customers);
        
        if(list==null || list.size()<1){
        	priveKey = PriveKeyUtil.generatePriveKey();
    		Date today = new Date(System.currentTimeMillis());
            customers.setId(priveKey);
            customers.setCreateDate(today);
            customers.setSource(Constant.CUSTOMERS_SOURCE_ONE);
            
    		//客户联系人库表 customers
            customersMapper.insertSelective(customers);
        } else {
        	
        	//返回客户信息
        	priveKey = list.get(0).getId();
        }

		return priveKey;
	}
	
	public List<Customers> queryCustomersByCondition(Customers customers){
		return customersMapper.queryCustomersByCondition(customers);
	}
	
	@Override
	public String insertCarService(CheckCar checkCar) {
		String priveKey = PriveKeyUtil.generatePriveKey();
		//预约车信息库表入库
		Vehicles vehicles = new Vehicles();
		vehicles.setId(priveKey);
		vehicles.setAddress(checkCar.getAddress());
		vehicles.setCustomerId(checkCar.getCustomerId());
		vehicles.setVin(checkCar.getEngineNo());
		vehicles.setOwner(checkCar.getOwner());
		vehicles.setPlateNo(checkCar.getPlateNo());
		vehicles.setRegisterDate(checkCar.getRegisterDate());
		vehicles.setVehicleType(checkCar.getVehicleType());
		vehiclesMapper.insertSelective(vehicles);
		
		return priveKey;
	}


	@Override
	public String checkCar(CheckCar checkCar) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String priveKey = PriveKeyUtil.generatePriveKey();
		Date today = new Date(System.currentTimeMillis());
		
		//订单信息库表入库
        Orders orders = new Orders();
        orders.setId(priveKey);
        orders.setCreateDate(sdf.format(today));
        orders.setIsPayed(Constant.ORDERS_ISPAYED_ONE);
        orders.setIsSuccess(Constant.ORDERS_ISSUCCESS_ONE);
        orders.setIsValid(Constant.ORDERS_ISVALID_ZERO);
        
        orders.setVehicleId(checkCar.getVehicleId());
        orders.setOrderDate(checkCar.getOrderDate());
        orders.setOrderTime(checkCar.getOrderTime());
        orders.setSource(Constant.CUSTOMERS_SOURCE_ONE);
        orders.setShopId(checkCar.getShopId());
        ordersMapper.insertSelective(orders);
        
		return priveKey;
	}
	
	/**
	 * 查询客户订单信息
	 */
	public List<CheckCar> queryMyCar(CheckCar checkCar){
		List<CheckCar> resultMyCheckCar = new ArrayList<CheckCar>();
		Customers queryCustomers = new Customers();
		queryCustomers.setId(checkCar.getCustomerId());
		//查询客户是否存在
		Customers customers = customersMapper.queryCustomersById(queryCustomers);
		if(customers!=null){
			Vehicles vehicles = new Vehicles();
			vehicles.setCustomerId(checkCar.getCustomerId());
			//根据客户Id查询客户车辆信息
			List<Vehicles> list = vehiclesMapper.queryVehiclesByCondition(vehicles);
			for(Vehicles tempVehicles:list){
				Orders orders = new Orders();
				orders.setVehicleId(tempVehicles.getId());
				
				CheckCar tempCheckCar = new CheckCar();
				
				//得到查询结果
				tempCheckCar.setMobile(customers.getMobile());
				tempCheckCar.setName(customers.getName());
				tempCheckCar.setVehicleType(tempVehicles.getVehicleType());
				tempCheckCar.setRegisterDate(tempVehicles.getRegisterDate());
				tempCheckCar.setPlateNo(tempVehicles.getPlateNo());
				tempCheckCar.setVin(tempVehicles.getVin());
				tempCheckCar.setOwner(tempVehicles.getOwner());
				tempCheckCar.setVehicleId(tempVehicles.getId());
				
				//查询车辆的验车信息
				List<Orders> ordersList = ordersMapper.queryOrdersByCondition(orders);
				for(Orders tempOrders : ordersList){
					Shops queryShops = new Shops();
					queryShops.setId(tempOrders.getShopId());
					//查询验车信息的车管所信息
					Shops shops = shopsMapper.queryShopsById(queryShops);
					
					tempCheckCar.setShopName(shops.getName());
					tempCheckCar.setOrderDate(tempOrders.getOrderDate());
					tempCheckCar.setIsValid(tempOrders.getIsValid());
					tempCheckCar.setIsSuccess(tempOrders.getIsSuccess());
				}
				
				resultMyCheckCar.add(tempCheckCar);
			}
		}
		return resultMyCheckCar;
	}
	
	/**
	 * 查询客户订单信息
	 */
	public List<CheckCar> queryMyCheckCar(CheckCar checkCar){
		List<CheckCar> resultMyCheckCar = new ArrayList<CheckCar>();
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			Customers queryCustomers = new Customers();
			queryCustomers.setId(checkCar.getCustomerId());
			//查询客户是否存在
			Customers customers = customersMapper.queryCustomersById(queryCustomers);
			if(customers!=null){
				Vehicles vehicles = new Vehicles();
				vehicles.setCustomerId(checkCar.getCustomerId());
				//根据客户Id查询客户车辆信息
				List<Vehicles> list = vehiclesMapper.queryVehiclesByCondition(vehicles);
				for(Vehicles tempVehicles:list){
					Orders orders = new Orders();
					orders.setVehicleId(tempVehicles.getId());
					
					CheckCar tempCheckCar = new CheckCar();
					//得到查询结果
					tempCheckCar.setMobile(customers.getMobile());
					tempCheckCar.setName(customers.getName());
					tempCheckCar.setVehicleType(tempVehicles.getVehicleType());
					tempCheckCar.setRegisterDate(tempVehicles.getRegisterDate());
					tempCheckCar.setPlateNo(tempVehicles.getPlateNo());
					tempCheckCar.setVin(tempVehicles.getVin());
					tempCheckCar.setOwner(tempVehicles.getOwner());
					tempCheckCar.setVehicleId(tempVehicles.getId());
					
					//查询车辆的验车信息
					List<Orders> ordersList = ordersMapper.queryOrdersByCondition(orders);
					
					for(Orders tempOrders : ordersList){
						Shops queryShops = new Shops();
						queryShops.setId(tempOrders.getShopId());
						//查询验车信息的车管所信息
						Shops shops = shopsMapper.queryShopsById(queryShops);
						//如果验车时间为当天，查询前方验车数量
						if(tempOrders.getOrderDate().equals(sdf.format(new Date(System.currentTimeMillis())))){
							Map<String,Object> queryMap = new HashMap<String,Object>();
							queryMap.put("orderDate", tempOrders.getOrderDate());
							queryMap.put("endTime", tempOrders.getCreateDate());
							queryMap.put("shopId", tempOrders.getShopId());
							queryMap.put("isSuccess", 1);//车辆预约没有验车的
							//查询此刻车辆数码
							tempCheckCar.setCheckFlag(1);//提醒客户需要进行验车
							tempCheckCar.setBeforeCountCar(ordersMapper.queryOrdersCountBefer(queryMap));
						} else {
							tempCheckCar.setCheckFlag(0);//不需要进行验车
						}
						
						String orderDate = tempOrders.getOrderDate();
						String currentDate = sdf.format(new Date(System.currentTimeMillis()));
						if(orderDate!=null && (sdf.parse(orderDate).getTime() >= sdf.parse(currentDate).getTime())){
							tempCheckCar.setOverdueFlag(1);//验车时间生效 未过期
						} else {
							tempCheckCar.setOverdueFlag(0);//已过期
						}
						
						tempCheckCar.setShopName(shops.getName());
						tempCheckCar.setOrderDate(tempOrders.getOrderDate());
						tempCheckCar.setIsValid(tempOrders.getIsValid());
						tempCheckCar.setIsSuccess(tempOrders.getIsSuccess());
					}
					
					resultMyCheckCar.add(tempCheckCar);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return resultMyCheckCar;
	}

	public List<Vehicles> queryVehiclesByCustomerId(Vehicles vehicles){
		return vehiclesMapper.queryVehiclesByCondition(vehicles);
	}

}
